package com.duke.shaking.profile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;

import com.duke.shaking.R;
import com.duke.shaking.ShakeTabActivity;
import com.duke.shaking.TitleActivity;
import com.duke.shaking.net.AsyncHttpResponseWithResultHandler;
import com.duke.shaking.net.NetUtils;
import com.duke.shaking.widget.FormRow;
import com.loopj.android.http.AsyncHttpClient;

public class InputBirthdayActivity extends TitleActivity implements OnDateSetListener {
	private final int DATE_DIALOG_ID = 1;
	private FormRow row_age;
	private FormRow row_constellation;
	private final int[] constellations_day = {20,19,21,21,21,22,23,23,23,23,22,22};
	private String[] constellations;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		row_age = (FormRow)findViewById(R.id.row_age);
		row_constellation = (FormRow)findViewById(R.id.row_constellation);
		constellations = getResources().getStringArray(R.array.constellations);
		
		showDialog(DATE_DIALOG_ID);
	}

	@Override
	protected int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_input_birthday;
	}

	@Override
	protected void onTitleInit() {
		// TODO Auto-generated method stub
		initTitle(0, //TITLE_BTN_EXIST | TITLE_BTN_BACK,
				R.string.back,
				TITLE_BTN_EXIST,
				R.string.next,
				R.string.title_input_birth);
	}
	
	public void onRightBtn(View v) {
		AsyncHttpResponseWithResultHandler responseHandler = new AsyncHttpResponseWithResultHandler(){

			@Override
			public void onResultSuccess(String response) {
				// TODO Auto-generated method stub
				super.onResultSuccess(response);
				go(SetAvatarActivity.class);
			}

			@Override
			public void onResultFailure(String result) {
				// TODO Auto-generated method stub
				super.onResultFailure(result);
				toast(R.string.err_update_profile);
			}
			
        };
        
        Map<String, String> params = new HashMap<String, String>();
        Date birthday = me.getBirth();
        if(birthday != null) {
        	SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
        	params.put(NetUtils.BIRTHDAY, format.format(birthday));
        }
        params.put(NetUtils.NICKNAME, me.getNickname());
        params.put(NetUtils.TOKEN, me.getAuthToken());
        AsyncHttpClient client = NetUtils.updateProfile(this, responseHandler, params, null);
        showProgress(R.string.progress_update_profile, client, responseHandler);
		
	}
	
	public void onBirthdayInput(View v) {
		info("onBirthdayInput");
		showDialog(DATE_DIALOG_ID);
	}

	public String getConstellation(int monthOfYear, int dayOfMonth) {
		int n = ((dayOfMonth > constellations_day[monthOfYear]) ? (monthOfYear + 1) : monthOfYear);
		if (constellations != null && n < constellations.length) {
			return constellations[n];
		}
		return "";
	}
	
	public void onDateSet(DatePicker picker, int year, int monthOfYear, int dayOfMonth) {
		Date birthday = new Date(year - 1900, monthOfYear, dayOfMonth);
		info("birthday set: " + birthday.toLocaleString());
		info("birthday monthOfyear: " + monthOfYear);
		Date today = new Date();
		long diff = 0, age = 0;
		if (today.getTime() < birthday.getTime()) {
			diff = 0;
		}
		else {
			diff = (today.getTime() - birthday.getTime()) / 1000;
		}
		age = diff / (24 * 3600);
		age /= 365;
		
		if (row_age != null) {
			row_age.setText(String.valueOf(age) + getResources().getString(R.string.age_year));
		}
		
		if (row_constellation != null) {
			row_constellation.setText(getConstellation(monthOfYear, dayOfMonth));
		}
		
		if(me != null) {
			me.setBirth(birthday);
		}
	}
	
	protected Dialog onCreateDialog(int id) {
		if (id == DATE_DIALOG_ID) {
			return new DatePickerDialog(this, this, 1986, 5, 1);
		}
		return null;
	}
}
